home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c++
- Subject: Re: help with cin.getline()
- Date: 18 Feb 1996 17:17:37 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4g7mvh$p4i@news1.usa.pipeline.com>
- NNTP-Posting-Host: pipe5.h1.usa.pipeline.com
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Feb 16, 1996 21:42:44 in article <help with cin.getline()>,
- 'preddym@ucs.orst.edu (M Preddy)' wrote:
-
-
- >Please forgive my newbie naivity, but I'm having trouble with
- >cin.getline() under MSVC++ 4.0. Why does the following snipit,
- >
- >
- > char cd_query[4];
- > do
- > {
- > cout << "Constant Cd (y/n)? ";
- > cin.getline(cd_query,4);
- > } while (cd_query[0] !='n' && cd_query[0] != 'y');
- >
- >
- >Produce the following output before stopping to wait for user input:
- >
- > Constant Cd (y/n)? Constant Cd (y/n)?
- >
- >The do-loop executes once, without stopping for input until the second
- >pass. This is obviously not what I want. Thank you for your help.
- >
-
- Actually, it does. Chances are that before the execution got
- to this point of your code, you did something like "cin >> somedata;".
- The pointer at that time was positioned at the newline character,
- which the extraction operator put back ino the stream after
- encountering it.
-
- When you call cin.getline(), it finds the newline, and returns an
- empty line. The next time around, it reads until the next newline
- which is the one youentered after your y/n response.
-
- How to handle this situation is left as an exercise.
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-